﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using BaseEntityApi;
using System.Collections.ObjectModel;
using System.IO;
using WPF.CustomControl;

namespace WPF各种控件样式
{
    /// <summary>
    /// Interaction logic for WPFTreeViewCtrl.xaml
    /// </summary>
    public partial class WPFTreeViewCtrl : Window
    {
        public WPFTreeViewCtrl()
        {
            InitializeComponent();
            InitDataToUI();

        }
        private List<TreeModel> CreateTree()
        {
            List<TreeModel> tem = new List<TreeModel>();


            tem.Add(new TreeModel() { ID = Guid.NewGuid().ToString(), Name = "Root1", Code = "01", Children = CreateTree(1), ItemIcon = imageIco, IsChecked = true });
            tem.Add(new TreeModel() { ID = Guid.NewGuid().ToString(), Name = "Root2", Code = "02", Children = CreateTree(2), ItemIcon = imageIco, IsChecked = false });
            tem.Add(new TreeModel() { ID = Guid.NewGuid().ToString(), Name = "Root3", Code = "03", Children = CreateTree(3), ItemIcon = imageIco, IsChecked = false });
            return tem;
        }
        private ObservableCollection<TreeModel> CreateTree(int i)
        {
            List<TreeModel> tem = new List<TreeModel>();
            TreeModel tree01 = new TreeModel() { ID = Guid.NewGuid().ToString(), Name = "Child01", Code = "01--1", ItemIcon = imageIco, IsChecked = true };
            TreeModel tree02 = new TreeModel() { ID = Guid.NewGuid().ToString(), Name = "Child02", Code = "02--2", ItemIcon = imageIco, IsChecked = true };
            TreeModel tree03 = new TreeModel() { ID = Guid.NewGuid().ToString(), Name = "Child03", Code = "03--3", ItemIcon = imageIco, IsChecked = false };
            tem.Add(tree01);
            tem.Add(tree02);
            tem.Add(tree03);
            return new ObservableCollection<TreeModel>(tem);
        }
        public static Stream Image = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WPF各种控件样式.Resources.t.png");
        public static string imageIco = "/WPF各种控件样式;component/Resources/tick.png";
        private void InitDataToUI()
        {
            List<TreeModel> treeList = CreateTree();

            #region CTreeView
            ctrl_Data1.CreateTreeViewDataBindColumns("Code", "Children", "IsChecked", "ItemIcon", new Thickness(2), Dochecked);

            ctrl_Data1.BindDataToTree<TreeModel>(new ObservableCollection<TreeModel>(treeList));
            ctrl_Data1.AddRightContexMenu("全选", "cb_00", Dochecked);
            ctrl_Data1.AddRightContexMenu("全选1", "cb_01", Dochecked);
            ctrl_Data1.AddRightContexMenu("全选2", "cb_02", imageIco, Dochecked);
            ctrl_Data1.AddRightMenuSeparator();
            ctrl_Data1.AddRightContexMenu("全选3", "cb_03", imageIco, Dochecked);
             
            #endregion

            #region TreeViewDataCtrl


            ctrl_Data.CreateTreeViewDataBindColumns("Code", "Children", "IsChecked", "ItemIcon", new Thickness(2), Dochecked);
            ctrl_Data.BindDataToTree<TreeModel>(new ObservableCollection<TreeModel>(treeList));

            ctrl_Data.AddRightContexMenu("全选", "cb_00", Dochecked);
            ctrl_Data.AddRightContexMenu("全选1", "cb_01", Dochecked);
            ctrl_Data.AddRightContexMenu("全选2", "cb_02", imageIco, Dochecked);
            ctrl_Data.AddRightMenuSeparator();
            ctrl_Data.AddRightContexMenu("全选3", "cb_03", imageIco, Dochecked);


            ctrl_Data2.CreateTreeViewDataBindColumns("Code", "Children", "IsChecked", "ItemIcon", new Thickness(2), Dochecked);
            ctrl_Data2.BindDataToTree<TreeModel>(new ObservableCollection<TreeModel>(treeList));
            #endregion
        }
        public void Dochecked(object sender, RoutedEventArgs e)
        { }

        public void CreateNewTree()
        {
            DataTemplate dTemplate = new DataTemplate();
            HierarchicalDataTemplate hDataTemplate = new HierarchicalDataTemplate();
            dTemplate = hDataTemplate;
            Binding hDataTemplateBind = new Binding();
            hDataTemplateBind.Path = new PropertyPath("Children");
            hDataTemplate.ItemsSource = hDataTemplateBind;
            FrameworkElementFactory FEFTB = new FrameworkElementFactory(typeof(TextBlock));
            Binding tbBind = new Binding();
            tbBind.Path = new PropertyPath("Name");
            FEFTB.SetBinding(TextBlock.TextProperty, tbBind);
            hDataTemplate.VisualTree = FEFTB;
            ctrl_Data1.ItemTemplate = dTemplate;
        }

        public void CtreateTreeItem(List<TreeModel> treeList)
        {
            foreach (TreeModel item in treeList)
            {
                TreeViewItem treeItem = new TreeViewItem();
                treeItem.Header = "Name";
                if (item.Children != null && item.Children.Count != 0)
                {
                    foreach (TreeModel child in item.Children)
                    {
                        CreateTreeItem(child, treeItem);
                    }
                }

                ctrl_Data1.Items.Add(treeItem);

            }
        }

        private void CreateTreeItem(TreeModel child, TreeViewItem treeItem)
        {
            TreeViewItem tree = new TreeViewItem();
            tree.Header = child.Name;
            if (child.Children != null && child.Children.Count != 0)
            {
                foreach (TreeModel item in child.Children)
                {
                    CreateTreeItem(item, treeItem);
                }
            }
            treeItem.Items.Add(tree);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            TreeModel tree = ctrl_Data.SelectedItem as TreeModel;
            if (tree == null)
            {
                TreeModel tem = new TreeModel();
                tem.Code = textBox1.Text.Trim();
                ObservableCollection<TreeModel> tsource = ctrl_Data.ItemsSource as ObservableCollection<TreeModel>;
                tsource.Add(tem);
            }
            else
            {
                TreeModel tem = new TreeModel();
                tem.Code = textBox1.Text.Trim();
                if (tree.Children == null)
                {
                    tree.Children = new ObservableCollection<TreeModel>();

                }
                tree.Children.Add(tem);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            TreeModel tree = ctrl_Data1.SelectedItem as TreeModel;
        }
    }

}
